package com.example.forum.service.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.BoardMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.Board;
import com.example.forum.service.IBoardService;
import com.example.forum.utils.IdNotNullUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2024/12/13
 */
@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        if (num <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return boardMapper.selectByNum(num);
    }

    @Override
    public void addOneArticleCountId(Long id) {
        //id输入有误
        if (id == null || id < 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询对应板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            log.warn(ResultCode.ERROR_IS_NULL.toString() + "board id : " + id);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //重新创建一个board对象作为更新对象传入动态sql（保证数据安全性和完整性）
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setUpdateTime(new Date());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //判断受影响的行数
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Board selectById(Long id) {
        //id输入有误
        if (id == null || id < 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        return boardMapper.selectByPrimaryKey(id);
    }

    @Override
    public void subOneArticleCountById(Long id) {
        IdNotNullUtil.idNotNullUtil(id);

        //查询板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
        }
        //构造更新对象
        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        if (updateBoard.getArticleCount() < 0) {
            updateBoard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row != 1) {
            throw new ApplicationException(ResultCode.ERROR_SERVICES.toString());
        }
    }
}
